﻿using ImTools;
using Prism.Commands;
using Prism.Common;
using Prism.Mvvm;
using Prism.Regions;
using Prism.Services.Dialogs;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WheelClean2.Model;
using WheelClean2.Mould;

namespace WheelClean.ViewModels
{
    class LaserParametersViewModel : BindableBase, IDialogAware
    {

      

        public event Action<IDialogResult> RequestClose;

        private LaserInfo _infos;
        /// <summary>
        /// 激光工艺实体
        /// </summary>
        public LaserInfo Info
        {
            get { return _infos; }
            set { SetProperty(ref _infos, value); }
        }

        public string Title => "激光参数编辑";

        /// <summary>
        /// 开始
        /// </summary>
        public DelegateCommand StartCommand { get; private set; }
        /// <summary>
        /// 开始
        /// </summary>
        public DelegateCommand StopCommand { get; private set; }
        /// <summary>
        /// 结束
        /// </summary>
        public DelegateCommand FinishCommand { get; private set; }
        /// <summary>
        /// 红光预览开启
        /// </summary>
        public DelegateCommand PreviewStartCommand { get; private set; }

        /// <summary>
        /// 红光预览关闭
        /// </summary>
        public DelegateCommand PreviewStopCommand { get; private set; }
        public DelegateCommand DelegateCommand_Save { get; private set; }
        private AppDbContext _content;
        private DaBiaoHelper daBiaoHelper;

        LaserParametersViewModel()
        {
            _content = new AppDbContext();
            DelegateCommand_Save = new DelegateCommand(Save);
            daBiaoHelper = DaBiaoHelper.Instance;
            StartCommand = new DelegateCommand(Start);
            StopCommand = new DelegateCommand(Stop);
            FinishCommand = new DelegateCommand(Finish);
            PreviewStartCommand = new DelegateCommand(PreviewStart);
            PreviewStopCommand = new DelegateCommand(PreviewStop);

        }
        private void PreviewStart()
        {
            foreach (var markInfo in daBiaoHelper.Marks)
            {
                markInfo.PreviewStart();
            }
        }
        private void PreviewStop()
        {
            foreach (var markInfo in daBiaoHelper.Marks)
            {
                markInfo.PreviewStop();
            }
        }

        private void Start()
        {
            foreach (var markInfo in daBiaoHelper.Marks)
            {
                markInfo.Start();
            }
        }

        private void Stop()
        {
            foreach (var markInfo in daBiaoHelper.Marks)
            {
                markInfo.Stop();
            }
        }

        private void Finish()
        {
            foreach (var markInfo in daBiaoHelper.Marks)
            {
                markInfo.Finish();
            }
        }

        private void Save()
        {
            if(Info.ID==0)
            {
                _content.LaserInfos.Add(Info);
            }
            else
            {
                _content.SaveChanges();
            }
           
        }

        public bool CanCloseDialog()
        {
            return true;
        }

        public void OnDialogClosed()
        {
          
        }

        public void OnDialogOpened(IDialogParameters parameters)
        {
           if(parameters.ContainsKey("type"))
            {
                string type = parameters.GetValue<string>("type");
                if(type=="add")
                {
                    Info = new LaserInfo();
                }
                else
                {
                    Info = _content.LaserInfos.FindFirst(t => t.ID == Convert.ToInt32(type));
                }
            }
        }
    }
}
